using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class TemplateFileDal : Base
    {
        public TemplateFileDal() : base() { }

        public enum Fields 
        {
            Id, 
            TemplateId,
            Name,
            Path,
            MasterPagePath,
            Theme,
            IsDefault
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            base.GetReaderValue<int>(Fields.Id, reader, table);
            base.GetReaderValue<int>(Fields.TemplateId, reader, table);
            base.GetReaderValue<string>(Fields.Name, reader, table);
            base.GetReaderValue<string>(Fields.Path, reader, table);
            base.GetReaderValue<string>(Fields.MasterPagePath, reader, table);
            base.GetReaderValue<string>(Fields.Theme, reader, table);
            base.GetReaderValue<bool>(Fields.IsDefault, reader, table);
        }

        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            return base.ExecuteGetItem(cmd);
        }

        public Hashtable GetDefault(int templateId)
        {
            SqlCommand cmd = CreateCommand("GetDefault");
            AddInputParameter(cmd, "TemplateId", templateId, SqlDbType.Int);
            return base.ExecuteGetItem(cmd);
        }

        public Hashtable GetByTemplateIdAndName(int templateId, string name)
        {
            SqlCommand cmd = CreateCommand("GetByTemplateIdAndName");
            AddInputParameter(cmd, "TemplateId", templateId, SqlDbType.Int);
            AddInputParameter(cmd, "Name", name, SqlDbType.VarChar, 50);
            return base.ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllByTemplateId(int templateId)
        {
            SqlCommand cmd = CreateCommand("GetAllByTemplateId");
            AddInputParameter(cmd, "TemplateId", templateId, SqlDbType.Int);
            return base.ExecuteGetCollection(cmd);
        }

        public void Update(ref int id, int templateId, string name, string path, string masterPagePath, string theme, bool isDefault, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "TemplateId", templateId, SqlDbType.Int);
            AddInputParameter(cmd, "Name", name, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "Path", path, SqlDbType.VarChar, 255);
            AddInputParameter(cmd, "MasterPagePath", masterPagePath, SqlDbType.VarChar, 255);
            AddInputParameter(cmd, "Theme", theme, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "IsDefault", isDefault, SqlDbType.Bit);
            AddInputParameter(cmd, "VersionId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "VersionIdOut", SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "VersionIdOut");
        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "VersionId", concurencyId, SqlDbType.Timestamp);
            ExecuteCommand(cmd);
        }
    }
}
